/*
 * Copyright 2014 by État de Genève (Switzerland)
 * All rights reserved.
 *
 * Author: ELCA Informatique SA
 */
define(function() {
   return {
       init: function(element, valueAccessor, allBindings) {
           var topMenuItem = ko.unwrap(valueAccessor());

           // the CSS class name of the section titles
           var titleClass = allBindings().formSectionClass;

           if(topMenuItem && topMenuItem.children && titleClass) {
               // normalize class dot prefix
               if(titleClass.indexOf('.') !== 0){
                   titleClass = '.' + titleClass;
               }

               var $allTitles = $(element).find(titleClass);

               // each children of the menu definition will be bound to an element, if this element exists
               _.each(topMenuItem.children, function(subItem, index) {
                   $allTitles.eq(index).text(subItem.label).attr('id', subItem.id);
               });
           }
       }
   };
});